package com.gz.mylife.util;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;

import com.gz.mylife.App;
import com.zl.smartmall.library.po.DeviceBean;

import android.content.Context;
import android.util.Log;

public class UdpReceiver {

	public static void startreciver(final Context context){
		Runnable runnable=new Runnable() {

			@Override
			public void run() {
				getupdreciver(context);
			}
		};
		Thread thread=new Thread(runnable);
		thread.start();
	}
	
	

	//判断Appcontext.devicelist是否有mac true代表没有 添加
	public static Boolean ifindexof(String mac){
		for(int c=0;c<App.devicelist.size();c++){
			if(mac.equals(App.devicelist.get(c).getDev_mac())){
				return  false;
			}
		}
		return  true;
	}

	public static void getupdreciver(Context context){
		while (true){
			try {
				byte[] recvBuf = new byte[1024];
				DatagramPacket recvPacket
				= new DatagramPacket(recvBuf , recvBuf.length);
				App.ds.receive(recvPacket);
				String 	recvStr = new String(recvPacket.getData() , 0 , recvPacket.getLength());
				Log.i("chenghao", "收到的信息＝"+recvStr);
				
				
				if("find".equals(App.type)){
					Log.i("chenghao", "存javabean");
					/**
					 * 回收信息＝
					 * ::1F:00:7D:03:23:52,
					 * Wifi,
					 * 001,
					 * 10.10.100.254,
					 * IO��
					 */
					String []devices=recvStr.split(",");
					String dev_mac=devices[0];
					if(ifindexof(dev_mac)){
						//可以添加
						DeviceBean bean=new DeviceBean();
						bean.setDev_mac(devices[0]);
						bean.setDev_type(devices[1]);
						bean.setDev_id(devices[3]);
						bean.setDev_ip(devices[4]);

						App.devicelist.add(bean);
					}
					Log.i("che", "List=="+App.devicelist.toString());
				} else if("open".equals(App.type)){
					Log.i("chenghao", "现在应该收到验证码 message="+recvStr);

					//在这就需要加密这个验证码 server 67  

					//wifi  回收信息＝::1F:00:7D:03:23:52,BD0529,OK
					//	if(recvStr.indexOf("OPENBEG")>0){
					if(recvStr.indexOf("OPENBEG")>0){
						String []charsplit=recvStr.split(",");
						String mac=charsplit[0];
						String index=charsplit[2];
						String mdstr=EncryptionUtils.encryptionString(index);
						String send  = mac+",OPEN,"+mdstr+","+App.sendtype;
						Log.i("chenghao", "fasong="+send );
						//send udp
						sendtoopen(send ,App.tagip);
					}
				}
			}
			catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
	
	private  static void sendtoopen(final String Str,final String ip) {
		Runnable runnable=new Runnable() {
			@Override
			public void run() {
				toSend(Str,"6996",ip,"");
			}
		};
		Thread thread=new Thread(runnable);
		thread.start();
	}

	/**
	 *
	 * @param message  发送内容
	 * @param port     端口号
	 * @param ip        ip地址
	 * type find 和send、  find是查设备 send是发送开门
	 */ 
	private  static void toSend(String message,String port,String ip,String type) {
		try {
			Log.i("chenghao","服务发送message＝"+message);
			Log.i("chenghao","服务发送port＝"+port);
			Log.i("chenghao","服务发送ip＝"+ip);
			//数据
			byte[] buf= message.getBytes();
			InetAddress destination = null ;
			//需要发送的地址
			destination = InetAddress.getByName(ip);
			int pro=Integer.parseInt(port);
			//(1), 内容 2，内容长度，3 ip地址， 4端口号
			DatagramPacket dp =new DatagramPacket(buf, buf.length, destination , pro);
			if(App.ds!=null){
				App.ds.send(dp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
